<?php
// $Id$

include_once(APP_LIB_DIR . 'GroupManagementModel.class.php');
require_once(APP_LIB_DIR . 'LookupModel.class.php');
include_once(APP_LIB_DIR . 'paging.class.php');
include_once(APP_LIB_DIR . 'JSON.php');

class GroupManagementAction extends Action
{
    function getRequestMethods ()
    {
        return REQ_GET|REQ_POST;
    }

    function isSecure ()
    {
        return false;
    }

    function getPrivilege (&$controller, &$request, &$user)
    {
        return Array('GROUPMANAGEMENT');
    }

    function getDefaultView (&$controller, &$request, &$user)
    {
        return VIEW_INPUT;
    }

    function execute (&$controller, &$request, &$user)
	{
		$auth = $user->getAttribute('auth');
		$GLOBALS['login'] = $auth['username'];
        return VIEW_INPUT;
	}

	function getlist($page,$search,$by,$sort)
	{
		$pager  = new Pager; 
		$sm     = new GroupManagementModel('GroupManagementAction');
		$json   = new Services_JSON();
		$search = isset($search['search']) ? $search['search'] : false;
    	        $search = !empty($search['searchtext']) ? $search : false;
	        $count  = $sm->collect(null,null,$search);
	   	$pager  = $pager->getPagerData($count[0][0],'', $page, "page");
		$offset = ($count[0][0] != 0) ? $pager->offset : 0;
	        $limit  = $pager->limit;
	        $link   = $pager->showPage;	
	        $list   = $sm->collect($offset,$limit,$search,$by,$sort);
		$data   = array($list,$link);
		return $json->encode($data); 
	}
	function lookup($what,$p1)
	{
		$lm     = new LookupModel('GroupManagementAction');
		$json   = new Services_JSON();
          
	    switch($what){
            	case 'get_members':
		return $json->encode($lm->members());break;
            default:
		return $json->encode($lm->members($p1));break;
          }
	}
	function save($detail, $id, $action)
	{
		$sm     = new GroupManagementModel('GroupManagementAction');
		$json   = new Services_JSON();

		if ($action == "get"){
			return $json->encode($sm->detail($id));
		}elseif ($action == "del"){
			//return $json->encode($sm->deactivate($id));
			$detail['modified_by'] = $GLOBALS['login'];
			$detail['active']      = 0;
			$detail['modified_date'] = "NOW()";
			return $json->encode($sm->edit($id,$detail));
		}elseif ($id){
			$detail = $this->change_case($detail['details']);
			$detail['modified_by'] = $GLOBALS['login'];
			return $json->encode($sm->edit($id,$detail));
		}else{
			$detail = $this->change_case($detail['details']);
			$detail['created_by'] = $GLOBALS['login'];
			$detail['creation_date'] = "NOW()";
			return $json->encode($sm->ins($detail));
		}
	}
	function change_case($array)
	{
		foreach ($array as $key => $val) $d[$key] = strtoupper($val);
		return $d;
	}
    function actions($action,$p1,$p2,$p3){
        $json   = new Services_JSON();
        $sm     = new LookupModel('GroupManagementAction');
	$gml     = new GroupManagementModel('GroupManagementAction');
        switch($action){
        case 'get_members':
	    return $json->encode($sm->members($p1));break;
        case 'get_memberslist':
	    return $json->encode($sm->members_list($p1));break;
        case 'save_gm':
             $gm = $p1;
             $gml->del_gmembers($p2);
             $gm = explode(',',$gm);
             foreach ($gm as  $num => $i){
               $det['group_id'] = $p2;
               $det['member_id']  = $i;
             $gml->add_gmembers($det);
             }
             return true;
        default:
            return $json->encode(false);
        }

    }
}

?>
